<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Encrypt extends CI_Encrypt {
	
	function encode_cookie($cookie, $key = ''){
		$key = $this->get_key($key);
		$enc = $this->_xor_encode($cookie, $key);
		
		if ($this->_mcrypt_exists === TRUE)
		{
			$enc = $this->mcrypt_encode($enc, $key);
		}
		return strtoupper(bin2hex($enc));
	}
	
	function decode_cookie($cookie, $key = ''){
		$key = $this->get_key($key);
		
		if (preg_match('/[^a-zA-Z0-9\/\+=]/', $cookie))
		{
			return FALSE;
		}

		//$dec = base64_decode($string);
		$dec = $this->hex2bin($cookie);
		if ($this->_mcrypt_exists === TRUE)
		{
			if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
			{
				return FALSE;
			}
		}

		return $this->_xor_decode($dec, $key);
	}
	
	function encode_transaction($string, $key = '')
	{
		return $string;
		
		//$strReal = strrev($string);	//Dao nguoc xau goc
		$strReal = $string;
		$strReal_encode = strtoupper(dechex($strReal)); //Chuyen sang he hexa
		
		/*
		 * Xau gay nhieu duoc tao bang cach:
		 * Cat xau goc tu ky tu thu 6, sau do noi voi xau goc
		 */
		$strCipher = substr($string, 0, 1).substr($string, strlen($string) - 1).$string;  
		$strCipher_encode = strtoupper(dechex($strCipher));	//Ma hoa xau nhieu
		
		/*
		 * Ket qua tra ve: 
		 * 3 ky tu dau cua strReal_encode + strCipher_encode + phan con lai cua strReal_encode + do dai cua strCipher_encode
		 */
		
		$strFull1 = substr($strReal_encode, 0, 3).$strCipher_encode.substr($strReal_encode, 3).strlen($strCipher_encode);
		$strFull2 = strrev(substr($strFull1, 0, 7)).strrev(substr($strFull1, 7));
		return $strFull2;
	}
	
	function decode_transaction($string, $key = '')
	{
		//return $string;
		
		if (!$string)
			return '';
		
		/*
		 * Lay lai ket qua ma hoa that cua xau goc da dao nguoc bang cach:
		 * 1. xac dinh xau goc dao nguoc da ma hoa:
		 * 	- xac dinh do dai xau nhieu theo ky tu cuoi
		 *  - bo xau nhieu tu vi tri thu 3 theo do dai da xac dinh
		 * 2. Giai ma xau gay nhieu de kiem tra
		 * 3. Giai ma ve dec va dao nguoc
		 */
		$string = strrev(substr($string, 0, 7)).strrev(substr($string, 7));
		$str_len = strlen($string);
		$strCipher_len = substr($string,$str_len-1);
		$strReal_encode = substr($string, 0, 3).substr($string, 3 + $strCipher_len, $str_len - 3 - $strCipher_len - 1);
		//$strOriginal = strrev(hexdec($strReal_encode));
		$strOriginal = hexdec($strReal_encode);
		$strCipher_encode = substr($string, 3, $strCipher_len);
		
		$strCipher = hexdec($strCipher_encode);
		if (substr($strCipher, 2) == $strOriginal)
			return $strOriginal;
		else
			return 'wrong_id';
	}
	
	/**
	 * Encode id
	 *
	 * @access	public
	 * @param	string	the string to encode
	 * @param	string	the key
	 * @return	string
	 */
	function id_encode($string, $key = '')
	{
		//return $string;		
		if(!$string)
			return '';		
		//$strReal = strrev($string);	//Dao nguoc xau goc
		$strReal = $string;
		$strReal_encode = strtoupper(dechex($strReal)); //Chuyen sang he hexa
		
		/*
		 * Xau gay nhieu duoc tao bang cach:
		 * ky tu dau cua xau goc + Cat xau goc tu ky tu thu 6 + xau goc
		 */
		$strCipher = substr($string, 0, 1).substr($string, strlen($string) - 1).$string;  
		$strCipher_encode = strtoupper(dechex($strCipher));	//Ma hoa xau nhieu
		
		/*
		 * Ket qua tra ve: 
		 * 3 ky tu dau cua strReal_encode + strCipher_encode + phan con lai cua strReal_encode + do dai cua strCipher_encode
		 */
		
		$strFull1 = substr($strReal_encode, 0, 3).$strCipher_encode.substr($strReal_encode, 3).strlen($strCipher_encode);
		$strFull2 = strrev(substr($strFull1, 0, 7)).strrev(substr($strFull1, 7));
		return $strFull2;
	}

	// --------------------------------------------------------------------
	
	/**
	 * Decode id
	 *
	 * @access	public
	 * @param	string
	 * @param	string
	 * @return	string
	 */
	function id_decode($string, $key = '')
	{
		//return $string;
		
		if (!$string)
			return '';
		
		/*
		 * Lay lai ket qua ma hoa that cua xau goc da dao nguoc bang cach:
		 * 1. xac dinh xau goc dao nguoc da ma hoa:
		 * 	- xac dinh do dai xau nhieu theo ky tu cuoi
		 *  - bo xau nhieu tu vi tri thu 3 theo do dai da xac dinh
		 * 2. Giai ma xau gay nhieu de kiem tra
		 * 3. Giai ma ve dec va dao nguoc
		 */
		$string = strrev(substr($string, 0, 7)).strrev(substr($string, 7));
		$str_len = strlen($string);
		$strCipher_len = substr($string,$str_len-1);
		$strReal_encode = substr($string, 0, 3).substr($string, 3 + $strCipher_len, $str_len - 3 - $strCipher_len - 1);
		//$strOriginal = strrev(hexdec($strReal_encode));
		$strOriginal = hexdec($strReal_encode);
		$strCipher_encode = substr($string, 3, $strCipher_len);
		
		$strCipher = hexdec($strCipher_encode);
		if (substr($strCipher, 2) == $strOriginal)
			return $strOriginal;
		else
			return 'wrong_id';
	}
	
	private function hex2bin($str) {
		$str = strtolower($str);
	    $bin = "";
	    $i = 0;
	    while ($i < strlen($str) - 1) {
	        $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
	        $i += 2;
	    }
	    return $bin;
	}
	
}